//spinecore.txt -  script for spinecores, the ubah-turret
//Cell 0,1 - Stuff done flag. If both 0, nothing. Otherwise If non-zero, this 
//turret wont attack. If the turret is itself attacked, will set the not-attack setting back to 0.
//Cell 2,3 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//Cell 4,5,6,7 - the creatures the spinecore can absorb to regain its life, in-game number
// Cell 8 - Can regrow its buds
begincreaturescript;

short i,target;
short healed_myself = 0;
short last_abil = 0;

body;

beginstate INIT_STATE;
	last_abil = get_current_tick();
	break;

beginstate DEAD_STATE;
	if ((get_memory_cell(2) != 0) || (get_memory_cell(3) != 0))
		set_flag(get_memory_cell(2),get_memory_cell(3),1);
break;

beginstate START_STATE; 
	if (who_shot_me() >= 0) {
		if ((get_memory_cell(0) != 0) || (get_memory_cell(1) != 0)) {
			if (get_attitude(ME) == 4)
				set_attitude(ME,10);
			set_flag(get_memory_cell(0),get_memory_cell(1),0);
			}

		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

	if ((get_memory_cell(0) != 0) || (get_memory_cell(1) != 0)) {
		if (get_flag(get_memory_cell(0),get_memory_cell(1)) > 0) {
			if (get_attitude(ME) >= 10) {
				set_attitude(ME,4);
				end();
				}
			}
			else if (get_flag(get_memory_cell(0),get_memory_cell(1)) == 0) {
				if (get_attitude(ME) == 4)
					set_attitude(ME,10);
				}
		}
		
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	healed_myself = 0;
	if ((get_health(ME) <= get_max_health(ME) / 2) && (last_abil != get_current_tick())) {
		if (get_memory_cell(4) > 0) {
			if (char_ok(get_memory_cell(4))) {
				kill_char(get_memory_cell(4),0);
				healed_myself = 1;
				}
			}
		if ((get_memory_cell(5) > 0) && (healed_myself == 0)) {
			if (char_ok(get_memory_cell(5))) {
				kill_char(get_memory_cell(5),0);
				healed_myself = 1;
				}
			}
		if ((get_memory_cell(6) > 0) && (healed_myself == 0)) {
			if (char_ok(get_memory_cell(6))) {
				kill_char(get_memory_cell(6),0);
				healed_myself = 1;
				}
			}
		if ((get_memory_cell(7) > 0) && (healed_myself == 0)) {
			if (char_ok(get_memory_cell(7))) {
				kill_char(get_memory_cell(7),0);
				healed_myself = 1;
				}
			}
		if (healed_myself) {
			print_str_color("The spinecore absorbs one of its buds.",2);
			last_abil = get_current_tick();
			pc_heard_sound(189);
			heal_char(ME,100);
			}
		}
		
	if ((get_memory_cell(0) != 0) || (get_memory_cell(1) != 0)) {
		if (get_flag(get_memory_cell(0),get_memory_cell(1)) > 0) {
			if (get_attitude(ME) >= 10)
				set_attitude(ME,4);
			set_foe_target(ME,-1);
			set_state(START_STATE);
			}
		}
		
	if (target_ok() == FALSE)
		set_state(START_STATE);
	if (dist_to_char(get_target()) > 8) {
		set_foe_target(ME,-1);
		set_state(START_STATE);
		}
	
	if ((healed_myself == 0) && (get_memory_cell(8) > 0) && (get_ran(1,0,100) < 40) &&
	  (last_abil != get_current_tick())) {
		if (char_ok(get_memory_cell(4)) == FALSE) {
			print_named_str(ME,"regrows a bud!");
			deduct_ap(8);
			pc_heard_sound(188);
			spawn_creature(get_memory_cell(4) - 8);
			}
			else if (char_ok(get_memory_cell(5)) == FALSE) {
				print_named_str(ME,"regrows a bud!");
				deduct_ap(8);
				pc_heard_sound(188);
				spawn_creature(get_memory_cell(5) - 8);
				}
			else if (char_ok(get_memory_cell(6)) == FALSE) {
				print_named_str(ME,"regrows a bud!");
				deduct_ap(8);
				pc_heard_sound(188);
				spawn_creature(get_memory_cell(6) - 8);
				}
			else if (char_ok(get_memory_cell(7)) == FALSE) {
				print_named_str(ME,"regrows a bud!");
				deduct_ap(8);
				pc_heard_sound(188);
				spawn_creature(get_memory_cell(7) - 8);
				}
		}
		
	last_abil = get_current_tick();
	do_attack();

	turret_heal();
break;

beginstate TALKING_STATE;
	print_str("All it really is is a big fungus. It can't communicate at all.");
break;